package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.model.TbGoods;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.uitl.JwtUtil;
import com.dongdongshop.vo.GoodsVo;
import com.dongdongshop.vo.ItemVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private GoodsService goodsService;


    @PostMapping
    public Result getAll() {
        List<GoodsVo> goodsVOList = goodsService.getAll();
        return Result.OK().setData(goodsVOList);
    }


    @DeleteMapping
    public Result deleteBatch(@RequestParam Long[] ids) {
        try {
            goodsService.deleteBatch(ids);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }

    @PutMapping
    public Result updateAuditStatus(@RequestParam List<Long> ids, @RequestParam String status) {
        try {
            goodsService.updateAuditStatus(ids, status);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }


    //添加商品(三表)
    @PostMapping("addTbGoods")
    public Result addTbGoods(@RequestHeader String AutharizeToken, @RequestBody GoodsVo tbGoods) {
        Claims claims = jwtUtil.parseJwt(AutharizeToken);
        String id = claims.getId();
        /*String subject = claims.getSubject();
        LoginUser parse = (LoginUser) JSONUtils.parse(subject);*/
        tbGoods.setSellerId(id);
        try {
            goodsService.add(tbGoods);
            return Result.OK();
        } catch (Exception e) {
            return Result.ER();
        }
    }

    //根据商品的id查询商品的详细信息，信息包含goods表中的数据 goods_desc数据，item数据
    @PostMapping("detail")
    public Result getGoodsDetail(Long goodsId){
        try {
            GoodsVo goodsVO = goodsService.getGoodsDetail(goodsId);
            return Result.OK().setData(goodsVO);
        }catch (Exception e){
            return Result.ER();
        }
    }

    //增加购物车 根据商品Id去查询商品的数据
    @GetMapping("getItem")
    public Result getItem(@RequestParam Long itemId){
        ItemVo vo = goodsService.getItem(itemId);
        return Result.OK().setData(vo);
    }

}
